import java.util.ArrayList;
import java.util.List;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    List<String> ret = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        //以形参形式传入 -> 函数自动“恢复现场”
        dfs(root, new StringBuilder());
        return ret;
    }

    public void dfs(TreeNode root, StringBuilder path_) {
        // if (root == null)
        //     return;
        //“恢复现场”
        StringBuilder path = new StringBuilder(path_);
        path.append(root.val);
        if (root.left == null && root.right == null) {
            ret.add(path.toString());
            return;
        }
        path.append("->");
        //if -> 剪枝
        if(root.left != null) dfs(root.left, path);
        if(root.right != null) dfs(root.right, path);
    }
}